Create 1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.js#3972
Open
aadil42 wants to merge 1 commit intoneetcode-gh:mainfrom
Open
Create 1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.js#3972aadil42 wants to merge 1 commit intoneetcode-gh:mainfrom
aadil42 wants to merge 1 commit intoneetcode-gh:mainfrom
Conversation
Solved number-of-ways-to-stay-in-the-same-place-after-some-steps
aakhtar3
reviewed
Apr 14, 2025
| @@ -0,0 +1,29 @@ | |||
| /** | |||
| * DP | Recursion | |||
| * Time O(n^2) | Space O(n^2) | |||
Collaborator
There was a problem hiding this comment.
Can reduce space
/**
* DP - Bottom Up
* Array - Tabulation
* Time O(N * MIN(N, M)) | Space O(MIN(N, M))
* @param {number} steps
* @param {number} arrLen
* @return {number}
*/
var numWays = (steps, arrLen) => {
const mod = ((10 ** 9) + 7);
const length = Math.min(((steps >> 1) + 1), arrLen);
let tabu = initTabu(length);
tabu[1] = 1;
for (let step = steps; (0 < step); step--) {
const prev = initTabu(length);
for (let pos = 1; (pos <= length); pos++) {
prev[pos] = (
tabu[pos - 1]
+ tabu[pos]
+ tabu[pos + 1]
) % mod;
}
tabu = prev;
}
return tabu[1];
};
var initTabu = (length) => new Array(length + 2).fill(0);
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Solved number-of-ways-to-stay-in-the-same-place-after-some-steps
File(s) Added: 1269-number-of-ways-to-stay-in-the-same-place-after-some-steps.js
Language(s) Used: JavaScript
Submission URL : https://leetcode.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/submissions/1589993538/